<template>
  <div class="chart" ref="main"></div>
</template>
<script>
import * as echarts from 'echarts'
import { getAction, postAction } from '@/api/manage'
export default {
  name: '',
  data () {
    return {
      nf: [],
      bardata: [],
      linedata: []
    }
  },
  mounted () {
    this.initData()
  },
  methods: {
    initData () {
      getAction('/topic/data/listAllBySql/dy_dp_csgl_cgsj_sjslbjl_qs', {}).then(res => {
        if (res.success) {
          res.result.map(item => {
            this.bardata.push(item.deal_num)
            this.linedata.push(item.deal_ratio)
            this.nf.push(item.year)
          })
          this.getChart(this.nf, this.bardata, this.linedata)
        }
      })
    },
    getChart (nf, bardata, linedata) {
      let myChart = echarts.init(this.$refs.main)
      let option

      option = {
        grid: {
          top: '15%',
          left: '1%',
          right: '2%',
          bottom: '0%',
          containLabel: true
        },
        legend: {
          padding: [10, 0],
          orient: 'horizontal',
          right: 120,
          itemGap: 12,
          data: ['进出口总额', '同比增速'],
          textStyle: {
            color: '#f9f9f9',
            borderColor: '#fff',
            fontSize: 12
          }
        },
        xAxis: [{
          nameGap: 5,
          type: 'category',
          axisLine: {
            show: true,
            lineStyle: {
              color: '#999'
            }
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: '#FFFFFF',
              fontSize: 10
            }
          },
          axisTick: {
            show: false
          },
          data: nf
        }],
        yAxis: [{
          name: '单位：亿元',
          type: 'value',
          nameTextStyle: {
            color: '#FFFFFF',
            fontSize: 12
          },
          min: 0,
          splitNumber: 7,
          splitLine: {
            show: true,
            lineStyle: {
              color: '#0a3256'
            }
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: '#FFFFFF',
              fontSize: 10
            }
          },
          axisTick: {
            show: false
          }
        },
        {
          name: '单位：%',
          nameTextStyle: {
            color: '#FFFFFF',
            fontSize: 12
          },
          type: 'value',
          splitLine: {
            show: true,
            lineStyle: {
              color: '#0a3256'
            }
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            color: '#ffffff',
            fontSize: 10
          }
        }],
        tooltip: {
          trigger: 'axis'
        },
        series: [{
          name: '同比增速',
          type: 'line',
          yAxisIndex: 1,
          symbolSize: 8, // 设置数据点的大小为10
          symbol: 'circle', // 设置数据点的形状为圆圈
          lineStyle: {
            color: '#55D297', // 线的颜色
            width: 2, // 线的宽度
            type: 'solid' // 线的类型，默认为实线
          },
          itemStyle: {
            color: '#55D297',
            borderColor: '#fff',
            borderWidth: 2 // 点的边框宽度
          },
          data: linedata
        },
        {

          name: '进出口总额',
          type: 'bar',
          barWidth: 15,
          label: {
            show: false,
            position: 'top',
            color: '#fff'
          },
          itemStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: '#49D5FF' },
              { offset: 1, color: '#548FDA' }
            ])
          },
          data: bardata
        }]
      }

      option && myChart.setOption(option)
    }
  }
}
</script>
<style lang="scss" scoped>
.chart {
  width: 100%;
  height: 200px;
}
</style>
